﻿class com.criticalmass.common.drawing.shapes.Wedge {
	
	var lineTo:Function;
	var moveTo:Function;
	var curveTo:Function;
	var beginFill:Function;
	var endFill:Function;
	var clear:Function;
	var lineStyle:Function;
	var beginGradientFill:Function;
/**
* This method draws dashed and dotted lines. It is an extension of the MovieClip Class
* @param x the starting x postion of the arc. This must be the current pen position (usually 0 unless a moveTo() was called)
* @param y the starting y postion of the arc. This must be the current pen position (usually 0 unless a moveTo() was called)
* @param radius the radius of the arc
* @param sweep the sweep of the arc. Negatives draw clockwise
* @param startAngle the length of each segment
* @param yRadius (optional) yRadius of the arc
* @param l (optional) a lineStyle object to use for the line consisting of thickness, col, and alpha
* @param c hex color of fill :: number or array [0x######,0x######]
* @param alpha alpha value of fill :: number or array [0x######,0x######]
* @param gradient type of gradient "linear" or "radial"
* @param ratios (optional :: default  [0,255]) - specifies the distribution of colors :: array [#,#];
* @param rot rotation of fill :: number or matrix object  {matrixType:"box",x:#,y:#,w:#,h:#,r:(#*(Math.PI/180))}
*/
	function drawWedge(x, y, radius, sweep, startAngle, yRadius, line, col, alpha, gradient, ratios, rot):Void {
		 if (yRadius == undefined || yRadius == null) {
			 yRadius = radius
		 }
		this.lineStyle(line.thickness, line.col, line.alpha)
			// if color is an object then allow for complex fills
			if(typeof col == "object")
			{
				if (typeof alpha != "object")
					var alphas = [alpha,alpha];
				else
					var alphas = alpha;

				if (ratios == undefined)
					var ratios = [ 0, 0xff ];

				var sh = yRadius *.7
				if (typeof rot != "object")
					var matrix = {matrixType:"box", x:-sh, y:sh, w:radius*2, h:yRadius*4, r:rot * 0.0174532925199433 }
				else
					var matrix = rot;

				if (gradient == "radial")
					this.beginGradientFill( "radial", col, alphas, ratios, matrix );
				else
					this.beginGradientFill( "linear", col, alphas, ratios, matrix );

			}
			else if (col != undefined)
			{
				this.beginFill (col, alpha);
			}
		 if (Math.abs(sweep) > 360) {
			 sweep = 360;
		 }
		 //Flash draws circles in 45 degree segments so we calculate how many segments are needed
		 var segs = Math.ceil(Math.abs(sweep)/45);
		 // the sweep of each segment
		 var segAngle = sweep/segs;
		 // convert from degrees to radians
		 var theta = -1*((segAngle/180)*Math.PI);
		 // convert start angle to radians
		 var angle = -1*((startAngle/180)*Math.PI);
		 //start points relative to x and y
		 var ax = x+Math.cos(-1*angle)*radius;
		 var ay = y+Math.sin(angle)*yRadius;
		 this.moveTo(x, y);
		 this.lineTo(ax, ay);
		 // if sweep is greater than 45 degrees draw out the arc in 45 degreee segments
		 // this is how the Flash Player draws circles
		 if (segs > 0) {
			 for (var i=0; i<segs; i++) {
				angle += theta
				var angleMid = angle - (theta/2);
				// end points
				 var bx = x + Math.cos(angle)*radius;
				 var by = y + Math.sin(angle)*yRadius;
				 // control points
				 var cx = x + Math.cos(angleMid) * (radius/Math.cos(theta/2));
				 var cy = y + Math.sin(angleMid) * (yRadius/Math.cos(theta/2));
				 //draw the segment
				 this.curveTo(cx, cy, bx, by);
			 }
			 this.lineTo(x, y);
		 }
		 if (col != undefined) {
			this.endFill();
		}
	}
/**
* The classConstructed method ensures that the MovieClip Class is extended with this new method
*/
	static function classConstruct(Void):Boolean {
		MovieClip.prototype.drawWedge = Wedge.prototype.drawWedge;
		return true;
	}
	static var classConstructed = classConstruct();
}